#include <windows.h>
#include <iostream>
#include <ctime>

using namespace std;

HANDLE default_heap;
HANDLE a_heap;
HANDLE b_heap;

DWORD WINAPI a_default_heap(LPVOID x)
{
	for (int i = 0; i < 100000; i++) {
	    void* p = HeapAlloc(default_heap, 0, 100);
		HeapFree(default_heap, 0, p);
	}
	return 0;
}

DWORD WINAPI b_default_heap(LPVOID x)
{
	for (int i = 0; i < 100000; i++) {
	    void* p = HeapAlloc(default_heap, 0, 100);
		HeapFree(default_heap, 0, p);
	}
	return 0;
}

DWORD WINAPI a_individual_heap(LPVOID x)
{
	for (int i = 0; i < 100000; i++) {
	    void* p = HeapAlloc(a_heap, 0, 100);
		HeapFree(a_heap, 0, p);
	}
	return 0;
}

DWORD WINAPI b_individual_heap(LPVOID x)
{
	for (int i = 0; i < 100000; i++) {
	    void* p = HeapAlloc(b_heap, 0, 100);
		HeapFree(b_heap, 0, p);
	}
	return 0;
}

void test_default_heap()
{
    default_heap = GetProcessHeap();

	clock_t c_start = clock();
	
	HANDLE a_thread = CreateThread(NULL, 0, a_default_heap, 0, 0, NULL);
	HANDLE b_thread = CreateThread(NULL, 0, b_default_heap, 0, 0, NULL);

	WaitForSingleObject(a_thread, INFINITE);
	WaitForSingleObject(b_thread, INFINITE);

	clock_t c_end = clock();

	cout << "time(default heap): " << c_end - c_start << endl;
}

void test_individual_heap()
{
	a_heap = HeapCreate(HEAP_NO_SERIALIZE, 1000, 2000);
	b_heap = HeapCreate(HEAP_NO_SERIALIZE, 1000, 2000);

	clock_t c_start = clock();
	
	HANDLE a_thread = CreateThread(NULL, 0, a_individual_heap, 0, 0, NULL);
	HANDLE b_thread = CreateThread(NULL, 0, b_individual_heap, 0, 0, NULL);

	WaitForSingleObject(a_thread, INFINITE);
	WaitForSingleObject(b_thread, INFINITE);

	clock_t c_end = clock();

	cout << "time(individual heap): " << c_end - c_start << endl;
	
	HeapDestroy(a_heap);
	HeapDestroy(b_heap);
}

int main()
{
	test_default_heap();
	test_individual_heap();
    
    return 0;
}
